
<template>
    
    <!-- 组件的模板 -->
    <div class="page team-info" style="min-height:0">
        <div class="weui-cells">
            <div class="weui-cell">
                <div class="weui-cell__bd">
                    <p><span class="title">姓名</span>{{info.userName}}</p>
                </div>                
            </div>
            <div class="weui-cell">
                <div class="weui-cell__bd">
                    <p><span class="title">电话</span>{{info.mobile}}</p>
                </div>                
            </div>
        </div>  
        <div class="weui-cells" v-if="admins.admin_delete_member">
            <a class="weui-cell weui-cell_access" v-if="admins.admin_change_member_role && info.userRole != '超级管理员'" @click="gotoRoleSelectList">
                <div class="weui-cell__bd">
                    <p><span class="title">角色</span>{{info.userRole}}</p>
                </div>
                <div class="weui-cell__ft">
                </div>
            </a>
            <a class="weui-cell weui-cell_access" v-else>
                <div class="weui-cell__bd">
                    <p><span class="title">角色</span>{{info.userRole}}</p>
                </div>
            </a>
            <a class="weui-cell weui-cell_access" @click="selectGroup">
                <div class="weui-cell__bd">
                    <p><span class="title">部门</span>{{prevGroupName}}</p>
                </div>
                <div class="weui-cell__ft">
                </div>
            </a>
        </div>
        <div class="weui-cells" v-else>
            <a class="weui-cell weui-cell_access">
                <div class="weui-cell__bd">
                    <p><span class="title">角色</span>{{info.userRole}}</p>
                </div>
            </a>
            <a class="weui-cell weui-cell_access">
                <div class="weui-cell__bd">
                    <p><span class="title">部门</span>{{prevGroupName}}</p>
                </div>
            </a>
        </div>
    </div> 
  
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import clientUi from 'gapi/clientUi.js'
    import teamApi from 'clientApi/teamApi.js'
    import * as util from 'js/util.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    import widget from 'gapi/widget.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                admins:{
                    admin_delete_member:false,
                    admin_change_member_role:false
                },
                teamid:'',
                info:{
                    userName:'',
                    mobile:'',
                    userRole:'普通员工',
                    roleid:'',
                    user_id:'',
                    department_id:'',
                    currentLoginUserId:''
                },
                prevGroupName:'',
                parent_id:'',
                id:''
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init 
             * @public
             * @return {Null} void
             */
            init (){
                this.teamid = util.getUrddataString('teamId') 
                this.info.user_id = util.getUrddataString('user_id') 
                this.info.department_id = util.getUrddataString('department_id') 
                this.info.userName = util.getUrddataString('name') 
                this.info.mobile = util.getUrddataString('mobile') 
                this.info.userRole = util.getUrddataString('role') 
                this.info.roleid = util.getUrddataString('roleid') 
                this.info.currentLoginUserId = util.getUrddataString('currentLoginUserId') 

                this.getPermissions()
                this.getGroupData()
                
            },
            getGroupData (){
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url:'/team/department_info',
                    type:'GET',
                    data:{
                        team_id:this.teamid, 
                        department_id:this.info.department_id
                    },
                    timeout:10000,
                    success:(obj) => {
                        console.log(obj)
                        commonPage.loadPageStatus('loadSuccess')
                        if(obj && obj.errno == 0){
                            this.prevGroupName = obj.data.name
                            this.parent_id = obj.data.parent_id
                            this.id = obj.data.id
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            //角色选择
            gotoRoleSelectList (){
                let encodeStr = encodeURIComponent(`teamId=${this.teamid}`)
                location.href = `dmsaas://SelectRole?urdData=${encodeStr}`
            },
            // 上级部门选择
            selectGroup(){
                let str = `teamid=${this.teamid}&backType=${1100}`
                let encodeStr = encodeURIComponent(str)
                location.href = `dmsaas://TeamDepartmentSelect?urdData=${encodeStr}`
            },
            //更新数据
            updateData (data){
                console.log(data)
                let currRoleId = ''
                let _data = ''
                // if(this.info.userRole == '管理员'){
                //     currRoleId = 2
                // }else if(this.info.userRole == '普通成员' || this.info.userRole == '成员'){
                //     currRoleId = 5
                // }else if(this.info.userRole == '超级管理员'){
                //     currRoleId = 1
                // }
                currRoleId = this.info.roleid
                if(data){
                    if(data.key == 'teamSelectRole'){
                        /*更改角色之后上报服务端*/
                        _data = {
                            team_id:this.teamid,
                            project_id:0,
                            user_id:this.info.user_id,  //当前用户的user_id
                            cur_user_role:currRoleId,  //当前用户选择角色的id
                            new_user_role:data.value.id,   // 修改之后返回的ID
                            department_id:this.info.department_id
                        }
                        
                    }else if(data.key == 'selectGroup'){
                        _data = {
                            team_id:this.teamid,
                            project_id:0,
                            user_id:this.info.user_id,  //当前用户的user_id
                            cur_user_role:currRoleId,  //当前用户选择角色的id
                            new_user_role:currRoleId,   // 修改之后返回的ID
                            department_id:data.value.id
                        }
                    }
                    util.ajax({
                        url:'/team/member/edit',
                        type:'POST',
                        data:_data,
                        timeout:10000,
                        success:(obj) => {
                            console.log(obj)
                            if(obj&&obj.errno == 0){
                                if(data.key == 'teamSelectRole'){
                                    this.info.userRole = data.value.name
                                    this.info.roleid = data.value.id
                                    widget.toast('角色修改成功')
                                    if(this.info.userRole == '普通成员' && this.info.currentLoginUserId == this.info.user_id){
                                        this.admins.admin_change_member_role = false
                                    }
                                }else if(data.key == 'selectGroup'){
                                    this.prevGroupName = data.value.name
                                    widget.toast('部门修改成功')
                                }
                                
                                teamApi.updateMember(); 
                            }else{
                                widget.toast(obj.errmsg)
                            }
                        },
                        error: (xhr, status) => {
                            widget.toast('服务器异常')
                        },
                        noNetwork: () => {
                            // 无网络的回调
                            widget.toast('网络连接失败，请检查网络')
                        }
                    })
                }
            },
             //权限获取
            getPermissions (){
                util.ajax({
                    url:'/permission/application',
                    type:'GET',
                    data:{
                        team_id:this.teamid,
                        project_id:0,
                        application_id:0
                    },
                    timeout:10000,
                    success:(obj) => {
                        // console.log(JSON.stringify(obj))
                        if(obj&&obj.errno == 0){
                            obj.data.forEach ( (i) => {
                                //判断是否有权对成员进行操作
                                if(i.id_name == 'admin_delete_member'){  
                                    this.admins.admin_delete_member = true   
                                }
                                if(i.id_name == 'admin_change_member_role'){
                                    this.admins.admin_change_member_role = true   
                                }
                            })
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            }
        },
        created(){
            this.init();
            commonPage.getWebViewID((data)=>{
                cache.setMemoryCache('memberInfoWebViewID',{'webViewID':data})
            })
            window.loadData = this.updateData
            /*设置Native的头部*/
            commonPage.updateTitleBar({
                backAction: 'goBack',
                leftAction: 'goBack',   // 点击左按钮的动作（即回调函数名称）
            })
            window.goBack = () => {
                let isAdmins = ''
                if(this.info.userRole == '普通成员' && this.info.currentLoginUserId == this.info.user_id){
                    isAdmins = 0
                    commonPage.goBack(1,{'key':'changeCurrRole','value':isAdmins})
                }else{
                    commonPage.goBack(1)
                }
                
            }
            
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/team.css"></style>



